﻿using Com.JunCaiSoft.Common.Controller;
using Com.JunCaiSoft.Common.Enums;
using Com.JunCaiSoft.Permission.Entity;
using Com.JunCaiSoft.Permission.IDao;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Com.JunCaiSoft.API.Permission.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class PositionController : BaseController<Position,Position,Position,Guid>
    {
        public PositionController(IPositionDao positiondao) : base(positiondao)
        {

        }
        /// <summary>
        /// 按职位ID批量删除
        /// </summary>
        /// <param name="Id">职位ID列表</param>
        /// <returns></returns>
        [HttpPatch]
        public virtual async Task<ActionResult> DeleteList(IEnumerable<Guid> Id)
        {
            try
            {
                await DaoAsync.dbContext.Database.BeginTransactionAsync();
                int result = DaoAsync.DeleteListAsync<Role>(x => Id.Contains(x.Id)).Result;

                return await Task.Run(() => Content(JsonConvert.SerializeObject(result)));
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                DaoAsync.dbContext.Database.CommitTransaction();
            }
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public virtual async Task<ActionResult> ListByPageAsync(int pageIndex, int pageSize)
        {
            try
            {
                var getList = await DaoAsync.ListByPageAsync(x => x.CreateTime, pageIndex, pageSize, OrderbyType.desc);

                return await Task.Run(() => Content(JsonConvert.SerializeObject(getList)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        /// <summary>
        /// 分页关键字查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        [HttpGet("search")]
        public virtual async Task<ActionResult> ListByPageAsync(int pageIndex, int pageSize, string keyword)
        {
            try
            {
                var getList = await DaoAsync.ListByPageAsync(x => x.CreateTime, pageIndex, pageSize, OrderbyType.desc);

                return await Task.Run(() => Content(JsonConvert.SerializeObject(getList)));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
